package com.cloudstudy.tool;

import org.springframework.core.env.Environment;

import javax.net.ssl.HttpsURLConnection;
import java.net.*;
import java.util.Enumeration;

/**
 * 从配置文件或 nacos 中获取环境变量
 * @author Tony
 * @since 2021/2/3
 */
public class EnvTool {

    // 默认全是 false
    public static boolean ISLOCALHOST;
    public static boolean ISDEVONLINE;
    public static boolean ISDEV;
    public static boolean ISSIT;
    public static boolean ISPROD;
    public static String ENV;
    public static String localIp = null;

    static {
        String active = get("spring.profiles.active");
        if (active.equalsIgnoreCase("test")){
            ISLOCALHOST = true;
        } else if (active.equalsIgnoreCase("devOnline")){
            ISDEVONLINE = true;
        } else if (active.equalsIgnoreCase("dev")){
            ISDEV = true;
        } else if (active.equalsIgnoreCase("sit")){
            ISSIT = true;
        } else if (active.equalsIgnoreCase("prod")){
            ISPROD = true;
        }
        ENV = active;

        localIp = getLocalIp();
    }

    public static String get(String property){
        Environment env = SpringTool.getSpringBean(Environment.class);
        return env.getProperty(property);
    }

//    /**
//     * 本地需要走代理，其他环境不用
//     * @author Tony
//     * @since 2021/2/3
//     */
//    public static HttpsURLConnection getConnectionByEnv(String url) throws Exception {
//        if (ISLOCALHOST){
//            String value = CacheTool.getSysParamValue(Cons.PROXY_TYPE_HTTP, true);
//            String[] arr = value.split(":");
//            LogTool.info(EnvTool.class, "env: {}, proxy: {}, url:{} ", EnvTool.ENV, value, url);
//            //创建代理服务器 http://gate-zrh-os.swissre.com:8080
//            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(arr[0], Integer.valueOf(arr[1])));
//            // 设定连接的相关参数
//            URL url2 = new URL(url);
//            HttpsURLConnection connection = (HttpsURLConnection) url2.openConnection(proxy);
//            return connection;
//        }
//
//        // 设定连接的相关参数
//        URL url2 = new URL(url);
//        HttpsURLConnection connection = (HttpsURLConnection) url2.openConnection();
//        return connection;
//    }


    /**
     * 获取本机IP地址
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String getLocalIp(){
        String localIp = "";
        Enumeration allNetInterfaces = null;
        try {
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        InetAddress ip = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
//			System.out.println(netInterface.getName());
            Enumeration addresses = netInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                ip = (InetAddress) addresses.nextElement();

                if (ip != null && ip instanceof Inet4Address) {

//					System.out.println("local IP address = " + ip.getHostAddress());
                    localIp = ip.getHostAddress();
                    if (!localIp.equals("127.0.0.1")) {
                        return localIp;
                    }

                }
            }
        }
        return localIp;
    }

}
